#ifndef DATASOURCE_H
#define DATASOURCE_H
#include "config.h"
#include <stdio.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>

template<class T>
void sort_statistics(const char *prefix, size_t times, T sort)
{	
	char fbuf[1024] = {0};
	sprintf(fbuf, "%s_ascend.dat", prefix);
	FILE *Fascend = fopen(fbuf, "w");
	
	sprintf(fbuf, "%s_descend.dat", prefix);
	FILE *Fdescend = fopen(fbuf, "w");
	
	sprintf(fbuf, "%s_random.dat", prefix);
	FILE *Frandom = fopen(fbuf,"w");
	int base = 5;
	int pow = 2;
	for(size_t i=0; i<times; i++) {
		size_t n = (base *= pow);
		int *buffer = new int[n];
		
		printf("i = %d\n",i);
		//完全逆序		
		for(size_t j=0; j<n; j++) {
			buffer[j] = n-j; //printf("%d,", buffer[j]);
		}
		
		fprintf(Fascend, "%d,%d,%f\n", i+1, n, sort(buffer, n));
//		printf("\n");
//		for(size_t j=0; j<n; j++) {
//			printf("%d,", buffer[j]);
//		}
		
		//完全正序
		for(size_t j=0; j<n; j++) {
			buffer[j] = j;
		}
		fprintf(Fdescend, "%d,%d,%f\n", i+1, n, sort(buffer, n));
		
		//随机序
		for(size_t j=0; j<n; j++) {
			buffer[j] = n-j;
		}
		
		boost::random::mt19937 gen;
		boost::random::uniform_int_distribution<> dist(1, n);
		for(size_t j=0; j<n; j++) {
			buffer[j] = dist(gen); //printf("%d,", buffer[j]);
		}
		
		fprintf(Frandom, "%d,%d,%f\n", i+1, n, sort(buffer, n));	
//		printf("\n");
//		for(size_t j=0; j<n; j++) {
//			printf("%d,", buffer[j]);
//		}	
		
		delete [] buffer;
	}
	
	fclose(Fascend);
	fclose(Frandom);
	fclose(Fdescend);
}

#endif // DATASOURCE_H
